package aTurk2;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

import aTurk.Parameters;

public class SimpleProblem implements Parameters {

	private ArrayList<Integer> xArray;
	private ArrayList<Double> pArray;
	public int cost;
	private int id;
	private int realVal;
	
	//cTor
	public SimpleProblem(int identifyer,Random randGen){
		id=identifyer;
		cost=randGen.nextInt(MAX_QUERY_TIME)+1;	//1-MAX_QUERY_TIME
		xArray=new ArrayList<Integer>();
		pArray=new ArrayList<Double>();
		
		ArrayList<Double> p=new ArrayList<Double>();
		double t2,sumP=0;
		for(int i=0;i<NUMBER_OF_BARS;i++){
			xArray.add(25);
			t2=randGen.nextDouble();
			while(t2<=0)
				t2=randGen.nextDouble();
			p.add(t2);
			sumP+=t2;
		}
		//nirmul
		for(int i=0;i<NUMBER_OF_BARS;i++){
			pArray.add(p.get(i)/sumP);
		}
		
		//setting the realValue
		setRealVal(randGen);
	}
	
	private void setRealVal(Random randGen){
		//configure the real value
		double tmpSumP=0,tmpSumL=0,prob,h;
		double randP=randGen.nextDouble();
		while(randP<=0)
			randP=randGen.nextDouble();
		//System.out.println("the rand is: "+randP);
		for(int i=0;i<NUMBER_OF_BARS;i++){
			tmpSumP+=pArray.get(i);
			tmpSumL+=xArray.get(i);
			if(tmpSumP>=randP){
				h=pArray.get(i)/(double)xArray.get(i);
				if(i>0){
					prob=randP-(tmpSumP-pArray.get(i));
					tmpSumL=tmpSumL-xArray.get(i);
				}else{
					prob=randP;
					tmpSumL=0;
				}
				//System.out.println("tmpsumL: "+tmpSumL+" prob: "+prob+" h: "+h);
				realVal=(int) (tmpSumL+(prob/h));
				break;
			}
		}
	}
	
	public String toSpecialFormat(){
		StringBuilder sb=new StringBuilder();
		sb.append(id);
		sb.append(",");
		sb.append(cost);
		sb.append(",");
		sb.append(realVal);
		
		DecimalFormat currency = new DecimalFormat("#0.000000");
		for(int i=0;i<xArray.size();++i){
			sb.append(",");
			sb.append(xArray.get(i));
			sb.append(",");
			sb.append(currency.format(pArray.get(i)));
		}

		return sb.toString();
	}
}
